package com.cloudptt.wp.ui.address.map;

import java.util.List;

import android.app.LoaderManager;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.amap.api.maps.AMap;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;
import com.cloudptt.api.comm.CommDlg;
import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.ProductConst;
import com.cloudptt.api.product.func.lbs.LbsListener;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.AddrOperator;
import com.cloudptt.api.product.operator.LBSOperator;
import com.cloudptt.wp.R;
import com.cloudptt.wp.ScreenManager;
import com.cloudptt.wp.SysApplication;
import com.cloudptt.wp.ui.BaseActivity;
import com.cloudptt.wp.ui.address.map.adapter.MapUserListAdapter;
import com.mcptt.db.loader.msg.MapPointListLoader;
import com.mcptt.db.loader.msg.MapPointListLoaderCb;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.MapPointInfo;
import com.mcptt.db.model.UserInfo;

/**
 * UI settings一些选项设置响应事件
 */
public class GaoDeMapActivity extends BaseActivity implements OnClickListener, LocationSource,OnItemClickListener,LbsListener,
AMapLocationListener,MapPointListLoaderCb
{
	private static String THIS_FILE = "GaoDeMapActivity";


	private AMap aMap;
	private MapView mapView;

	private TextView backView;
	private TextView localView;
	//	private ScrollView userListScrollView;
	private ListView userListView;

	//	private MapUserListAdapter adapter;

	private boolean isShow = true;

	private OnLocationChangedListener mListener;
	private LocationManagerProxy aMapManager;

	private List<UserInfo> userList = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);
		/*
		 * 设置离线地图存储目录，在下载离线地图或初始化地图设置; 使用过程中可自行设置, 若自行设置了离线地图存储的路径，
		 * 则需要在离线地图下载和使用地图页面都进行路径设置
		 */
		// Demo中为了其他界面可以使用下载的离线地图，使用默认位置存储，屏蔽了自定义设置
		// MapsInitializer.sdcardDir =OffLineMapUtils.getSdCacheDir(this);

		initView(savedInstanceState);
		ScreenManager.getScreenManager().pushActivity(this);
	}

	/**
	 * 初始化AMap对象
	 */
	private void initView(Bundle savedInstanceState) {

		mapView = (MapView) findViewById(R.id.map);
		mapView.onCreate(savedInstanceState);// 此方法必须重写

		if (aMap == null) {
			aMap = mapView.getMap();
		}

		backView = (TextView) findViewById(R.id.backView);
		backView.setOnClickListener(this);

		localView = (TextView) findViewById(R.id.localView);
		localView.setOnClickListener(this);

		//		userListScrollView = (ScrollView) findViewById(R.id.userListScrollView);

		userListView = (ListView) findViewById(R.id.userListView);
		userListView.setOnItemClickListener(this);

		initOrUpdateLoader(false);

		//		initOrUpdateData();
	}

	//	private void initOrUpdateData() 
	//	{
	////		SysApplication app = ((SysApplication) getApplicationContext());
	////		Product product = app.getProduct();
	////		AddrOperator operator = (AddrOperator)product.getOperator(ProductConst.Func_Addr);
	////		List<UserInfo> list = operator.getAddressList(addrType);
	//		
	//		adapter = new MapUserListAdapter(this, userList,null);
	//		userListView.setAdapter(adapter);
	//	}

	/**
	 * 方法必须重写
	 */
	@Override
	protected void onResume() {
		super.onResume();
		mapView.onResume();
//		SysApplication app = ((SysApplication) getApplicationContext());
//		Product product = app.getProduct();
//		product.setLbsListener(this);
	}

	/**
	 * 方法必须重写
	 */
	@Override
	protected void onPause() {
		super.onPause();
		mapView.onPause();
		deactivate();
	}

	/**
	 * 方法必须重写
	 */
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mapView.onSaveInstanceState(outState);
	}

	/**
	 * 方法必须重写
	 */
	@Override
	protected void onDestroy() {
		super.onDestroy();
		mapView.onDestroy();
	}


	@Override
	public void onClick(View view) {
		switch (view.getId()) 
		{
		case R.id.backView:
			ScreenManager.getScreenManager().popActivity();
			break;
		case R.id.localView:
			if(userListView != null)
			{
				isShow = !isShow;
				Log.i(THIS_FILE," onClick isShow = " + isShow);
				if(isShow)
				{
					userListView.setVisibility(View.VISIBLE);
				}
				else
				{
					userListView.setVisibility(View.GONE);
				}
			}

			break;
		default:
			break;
		}
	}

	@Override
	public void onLocationChanged(Location location) {

	}

	@Override
	public void onProviderDisabled(String provider) {

	}

	@Override
	public void onProviderEnabled(String provider) {

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {

	}

	/**
	 * 定位成功后回调函数
	 */
	@Override
	public void onLocationChanged(AMapLocation aLocation) {
		if (mListener != null) {
			mListener.onLocationChanged(aLocation);// 显示系统小蓝点
		}
	}

	/**
	 * 激活定位
	 */
	@Override
	public void activate(OnLocationChangedListener listener) {
		mListener = listener;
		if (aMapManager == null) {
			aMapManager = LocationManagerProxy.getInstance(this);
			/*
			 * mAMapLocManager.setGpsEnable(false);//
			 * 1.0.2版本新增方法，设置true表示混合定位中包含gps定位，false表示纯网络定位，默认是true
			 */
			// Location API定位采用GPS和网络混合定位方式，时间最短是2000毫秒
			aMapManager.requestLocationUpdates(
					LocationProviderProxy.AMapNetwork, 2000, 10, this);
		}
	}

	/**
	 * 停止定位
	 */
	@Override
	public void deactivate() {
		mListener = null;
		if (aMapManager != null) {
			aMapManager.removeUpdates(this);
			aMapManager.destory();
		}
		aMapManager = null;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		if(userList != null)
		{
			AddrInfo info = userList.get(position);
			//doRequestUserSerialLocation(info);
		}
	}

//	private void doRequestUserSerialLocation(UserInfo info)
//	{
//		Log.i(THIS_FILE," doRequestUserSerialLocation info = " + info);
//		SysApplication app = ((SysApplication)this.getApplicationContext());
//		Product product = app.getProduct();
//		LBSOperator operator = (LBSOperator)product.getOperator(ProductConst.Func_Local);
//		if(operator != null)
//		{
//			String num = info.getMcId();
//			int result = CauseCont.OperateErr_Exception;
//			num = info.getMcpttId();
//			result = operator.requestUserSerialLocation(num, 10, 10);
//			if(result != CauseCont.OperateCheck_Suc) 
//			{
//				String errStr = CauseCont.getOperateCheckErrStr(result,this);
//				CommDlg.showToast(this,errStr);
//				return;
//			}
//		}
//	}

	@Override
	public void recvNewReport() {
		// TODO Auto-generated method stub
		CommDlg.showShortToast(this, "recvNewReport");
		initOrUpdateLoader(true);
	}

	@Override
	public void onLoadFinished(List<MapPointInfo> pointList) {
		// TODO Auto-generated method stub
		Log.v( THIS_FILE, "onLoadFinished pointList = " + pointList);
		SysApplication app = ((SysApplication) getApplicationContext());
		Product product = app.getProduct();
		AddrOperator operator = (AddrOperator)product.getOperator(ProductConst.Func_Addr);
		userList = operator.getUserInfoList();

//		userList = PlugTestConfigInfo.getInstance().getAddressList(UserInfo.UserType_User);
		MapUserListAdapter adapter = new MapUserListAdapter(this, userList,pointList);
		userListView.setAdapter(adapter);
	}

	private void initOrUpdateLoader(boolean isUpdate)
	{
		Log.v( THIS_FILE, "initOrUpdateLoader " + isUpdate);
		SysApplication app = ((SysApplication) getApplicationContext());
		Product product = app.getProduct();
		LBSOperator operator = (LBSOperator)product.getOperator(ProductConst.Func_Local);
//		UserInfo curUserInfo = product.getMcpttLoginInfo().toUserInfo();
		MapPointListLoader loader = operator.geMapPointListLoaderCb(this, this);
		if(isUpdate)
		{
			LoaderManager manager = getLoaderManager();
			if(manager != null)
			{
				manager.restartLoader(0, null,loader);
			}
		}
		else
		{
			LoaderManager manager = getLoaderManager();
			if(manager != null)
			{
				manager.initLoader(0, null,loader);
			}
		}
	}

	@Override
	public void onLoaderReset() {
		// TODO Auto-generated method stub
		userListView.setAdapter(null);
	}


}
